<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController {

    public function auditoria($accion, $idUsuario) {
        $model = new AditoriaSesiones();
        $model->fecha = date(Yii::app()->params['fechaMysql']);
        $model->ipAcceso = $this->getRealIP();
        $model->accion = $accion;
        $model->idUsuario = $idUsuario;

        if ($model->save())
            return true;

        return false;
    }

    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    private $accesos = array();

    public function obtenerPrivilegio() {
        $this->accesos['pb'] = "true";
        return $this->accesos;
        //return isset($this->accesos[$seccion]) ? $this->accesos[$seccion]:"false";
    }

    /* public function acceder(){
      $this->accesos['pb'] = "true";
      //Obtiene XML y obtiene reglas
      return true;
      } */

    public $layout = '//layouts/column1';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu = array();

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();

    function getRealIP() {
        if (!empty($_SERVER['HTTP_CLIENT_IP']))
            return $_SERVER['HTTP_CLIENT_IP'];

        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
            return $_SERVER['HTTP_X_FORWARDED_FOR'];

        return $_SERVER['REMOTE_ADDR'];
    }

    function json_encode_with_relations(array $models, $attributeNames) {
        $attributeNames = explode(',', $attributeNames);

        $rows = array(); //the rows to output
        foreach ($models as $model) {
            $row = array(); //you will be copying in model attribute values to this array 
            foreach ($attributeNames as $name) {
                $name = trim($name); //in case of spaces around commas
                $row[] = CHtml::value($model, $name); //this function walks the relations 
            }
            $rows[] = $row;
        }
        //header('Content-type:application/json');
        return CJSON::encode(array( "aaData" => $rows));
        //exit(); //or Yii::app()->end() if you want the log to show in debug mode
    }

}